/**
 * 
 */
package br.com.binding.client.mock;

import java.util.ArrayList;
import java.util.List;

import br.com.binding.client.bind.BindingView;
import br.com.binding.client.bind.annotations.Action;
import br.com.binding.client.bind.annotations.ActionBind;
import br.com.binding.client.bind.annotations.Bound;
import br.com.binding.client.bind.annotations.BoundProperty;
import br.com.binding.client.bind.annotations.TextBind;
import br.com.binding.client.bind.annotations.values.WhenText;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

/**
 * @author Marcelo Emanoel
 * 
 */
public class FormView extends Composite implements BindingView{
	
	@TextBind(model="bean", name="name", when=WhenText.ON_KEY_DOWN)
	protected TextBox nameField;
	
	@TextBind(model="bean", name="surName", when=WhenText.ON_KEY_UP)
	protected TextBox surNameField;
	
	protected Label beanNameLabel;
	protected Label beanSurNameLabel;
	
	@ActionBind(method="clear")
	protected Button clearButton;
	
//	@LoadAction("loadValues")
//	@SelectionBind(name="selectedValue")
//	protected ListBox valuesListBox;
	
	@Bound(properties={
			@BoundProperty(name="name", viewProperty="beanNameLabel"),
			@BoundProperty(name="surName", viewProperty="beanSurNameLabel"),
//			@BoundProperty(name="selectedValue", viewProperty="valuesListBox")
	})
	protected FormBean bean;

	public FormView() {
		init();
		layout();
	}
	
	private void init() {
		nameField = new TextBox();
		surNameField = new TextBox();
//		valuesListBox = new ListBox();
		beanNameLabel = new Label();
		beanSurNameLabel = new Label();
		clearButton = new Button("Clear");
		
		bean = GWT.create(FormBean.class);
	}

	private void layout() {
		FlexTable table = new FlexTable();
		table.setWidget(0, 0, nameField);
		table.setWidget(0, 1, beanNameLabel);
		table.setWidget(1, 0, surNameField);
		table.setWidget(1, 1, beanSurNameLabel);
		table.setWidget(2, 1, clearButton);
		initWidget(table);
	}
	
	public List<String> loadValues(){
		ArrayList<String> strings = new ArrayList<String>();
		strings.add("a");
		strings.add("b");
		strings.add("c");
		strings.add("d");
		return strings;
	}
	
	@Action()
	public void clear(){
		bean.setName("");
		bean.setSurName("");
	}
}